#include "SequenceStaticList.h"

void InitList(List * L)
{
     L->last=-1;
}

Node GetData(const List * L,int i)
{
     Node temp;
     temp=L->elem[i-1];
     return temp;
}

int Locate(const List * L,const char name[])
{
    int i=0;
    while((i<=L->last)&&strcmp(L->elem[i].name,name)!=0)
        i++;
    if(i<=L->last)
        return i+1;
    else
        return -1;
}

bool InsList(List * L,int i,Node node)
{
     int j;

     if(L->last>=SIZE-1)
     {
         printf("List is full,can't insert!");
         return false;
     }
     if((i<1)||(i>L->last+2))
     {
         printf("Insert position illegal!");
         return false;
     }
     for(j=L->last;j>=i-1;j--)
        L->elem[j+1]=L->elem[j];
     L->elem[i-1]=node;
     L->last++;
     return true;
}

bool DelList(List * L,int i,Node * node)
{
     int j;

     if(L->last==-1)
     {
         printf("List is empty, can't delete!");
         return false;
     }
     if((i<1)||(i>L->last+1))
     {
         printf("Remove location illegal!");
         return false;
     }
     *node=L->elem[i-1];
     for(j=i;j<=L->last;j++)
        L->elem[j-1]=L->elem[j];
     L->last--;
     return true;
}

void output(const List * L)
{
     int i;
     for(i=0;i<L->last+1;i++)
     {
         printf("name:%-10s sex:%-10s age:%-10d\n",L->elem[i].name,L->elem[i].sex,L->elem[i].age);
     }
}

int main()
{
    List L;           //创建一个顺序表L
    int i,j,k,l,n,m;
    char name[SIZE];  //用于按值查找算法
    Node temp1;       //用于按序号查找算法
    Node node1;       //用于插入算法
    Node node2;       //用于删除算法，保存被删元素的值

    InitList(&L);
    printf("请输入信息的个数:");
    scanf("%d",&n);
    getchar();      //处理scanf()遗留在缓冲区的换行符
    for(i=0;i<n;i++)
    {
        puts("Please enter the name of a person:");
        gets(L.elem[i].name);
        puts("Please enter a person's sex:");
        gets(L.elem[i].sex);
        puts("Please enter a person's age:");
        scanf("%d",&L.elem[i].age);
        getchar();  //处理scanf()遗留在缓冲区的换行符
        L.last=L.last+1;
    }
    output(&L);

    printf("Please enter the want to find the serial number:");
    scanf("%d",&j);
    temp1=GetData(&L,j);
    printf("The serial number is the information for %d:name:%-10s sex:%-10s age:%-10d\n"
           ,j,temp1.name,temp1.sex,temp1.age);

    printf("Please enter a search name:");
    getchar();  //处理scanf()遗留在缓冲区的换行符
    gets(name);
    k=Locate(&L,name);
    if(k!=-1)
    printf("Information for %s: sex:%-10s age:%-10d\n"
           ,name,L.elem[k-1].sex,L.elem[k-1].age);
    else
    printf("Query error!");

    printf("Please input to insert position:");
    scanf("%d",&l);
    while(getchar()!='\n')  //处理scanf()遗留在缓冲区的换行符
          continue;
    printf("Please enter the insert name:");
    gets(node1.name);
    printf("Please input into a person's sex:");
    gets(node1.sex);
    printf("Please input into a person's age:");
    scanf("%d",&node1.age);
    InsList(&L,l,node1);
    output(&L);

    printf("Please enter the number you want to delete:");
    scanf("%d",&m);
    DelList(&L,m,&node2);
    output(&L);

    printf("Bye!");

    return 0;
}
